<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class ZonesController extends AppController {


	public $uses = array('Zone');
	public $components = array('Paginator');

	public function index(){
		//var_dump($this->Session->read('User'));
		$pagination_settings= array();
		if(isset($this->data['keyword'])){
			$keyword = $this->data['keyword'];
			$pagination_settings['conditions']= array('Clients.dni LIKE' => "$keyword%");
			
		}

		$pagination_settings['limit'] =30;
		$this->Paginator->settings =$pagination_settings;
    $categories = $this->Paginator->paginate('Zone');
		$this->set('data',$categories);	
	}


	public function add(){


		if(!empty($this->data)){
			$this->Zone->set($this->data);			
			if($this->Zone->validates())
			{				
				if($this->Zone->save($this->data)){
					$this->redirect('/zones');
				}				
			}
			else{
				$errors = $this->User->validationErrors;
				$this->Session->setFlash(__("Verifique los valores ingresados, algun dato es incorrecto", true), 'flash', array('class' => 'alert alert-error'));
			}
		}
	}

	public function edit($id){


		if(!empty($this->data)){
			$this->Zone->id = $id;
      if ($this->Zone->save($this->data)) {
          //$this->Session->setFlash('Cliente Actualizado.', 'flash', array('class' => 'thrill'));
          $this->redirect('/zones');
      }
		}
		else {
        $this->request->data = $this->Zone->findById($id);
    }
    	$this->render('add');
	}
	public function delete(){
		$this->layout=false;
		$this->render(false);
		$this->Zone->delete($this->request->data('ids'));
		echo json_encode(true);
	}
}
